package com.dgw.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dgw.entity.Course;
import com.dgw.entity.StudentCourseRel;
import com.dgw.service.CourseService;
import com.dgw.utils.PageRows;
import com.dgw.utils.Result;
import com.dgw.utils.ResultUtil;
import com.dgw.vo.CourseVO;
import com.github.pagehelper.PageInfo;

@Controller
public class CourseController {
	
	
	@Autowired
	private CourseService courseService;
	
	/**
	 * 跳转课程列表
	 * @return
	 */
	@RequestMapping(value = "/course", method = RequestMethod.GET)
	public String course(){
		return "course/course_list";
	}
	
	/**
	 *  查询课程列表
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/course/list", method = RequestMethod.GET)
	@ResponseBody
	public Result<PageInfo<Course>> courseList(String keyword,PageRows pageRows) throws UnsupportedEncodingException{
		keyword= new String(keyword.getBytes("iso-8859-1"),"utf-8");
		PageInfo<Course> courses = courseService.selectByPageAndSelections(keyword, pageRows);
		return ResultUtil.success(courses);
	}
	
	/**
	 *  查询课程列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/course/add", method = RequestMethod.GET)
	public String courseAdd(){
		return "course/course_add";
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/course/add", method = RequestMethod.POST)
	@ResponseBody
	public Result courseSave(Course course){
		int insert = courseService.insert(course);
		if(insert>0){
			return ResultUtil.success();
		}
		return ResultUtil.error(444, "保存失败");
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/course/edit", method = RequestMethod.GET)
	public String courseEdit(String cId,Model model){
		Course course = courseService.selectByPrimaryKey(cId);
 		model.addAttribute("course", course);
		return "course/course_edit";
	}
	

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/course/edit", method = RequestMethod.POST)
	@ResponseBody
	public Result courseUpdate(Course course){
		int insert = courseService.updateByPrimaryKeySelective(course);
		if(insert>0){
			return ResultUtil.success();
		}
		return ResultUtil.error(444, "修改失败");
	}
	
	
	/**
	 * 跳转课程分配列表
	 * @return
	 */
	@RequestMapping(value = "/course/distribute", method = RequestMethod.GET)
	public String courseDistribute(String sId,Model model){
		List<Course> courseListn = courseService.selectAll();
		List<StudentCourseRel>  studentCourses= courseService.selectStudentCourseRel(sId);
		List<CourseVO> courseVOs= new ArrayList<CourseVO>();
		for(Course c:courseListn){
			CourseVO courseVO = new CourseVO();
			courseVO.setcId(c.getcId());
			courseVO.setcName(c.getcName());
			for(StudentCourseRel s:studentCourses){
				if(c.getcId().equals(s.getcId())){
					courseVO.setChecked(true);
				}
			}
			courseVOs.add(courseVO);
		}
		model.addAttribute("courseList",courseVOs);
		model.addAttribute("sId",sId);
		return "course/course_distribute";
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/course/save", method = RequestMethod.POST)
	@ResponseBody
	public Result courseUpdate(String sId,String[] cIds){
		int insert = courseService.updateStudentCourse(sId, cIds);
		if(insert>0){
			return ResultUtil.success();
		}
		return ResultUtil.error(444, "修改失败");
	}
	
	
	
}
